<?php
class Gen_bill_model extends CI_Model {

  public function __construct()
  {
    $this->load->database();
  }

  /*
    create table gen_bill (
      bill_id int(11) not null AUTO_INCREMENT,
      bill_json text,
      name varchar(128),
      phone varchar(128),
      address varchar(256),
      note text,
      time varchar(128),
      primary key (bill_id)
    );

  */

  public function get_bills()
  {
    $this->db->order_by("time", "desc"); 
    $query = $this->db->get('gen_bill');
    return $query->result_array();
  }  

  public function get_con_bills($store_id, $fromdate, $todate, $max, $page)
  {
    if ($store_id > -1)
      $query = $this->db->where('store_id', $store_id);
    $query = $this->db->where('time >= ', strtotime($fromdate) );
    $query = $this->db->where('time <= ', strtotime($todate) + 3600 * 24);
    $query = $this->db->order_by('bill_id', 'desc');
    $query = $this->db->get('gen_bill', $max, ($page - 1) * $max);
    return $query->result_array();
  }

  public function get_con_bills_nums($store_id, $fromdate, $todate, $max, $page)
  {
    if ($store_id > -1)
      $query = $this->db->where('store_id', $store_id);
    $query = $this->db->where('time >= ', strtotime($fromdate) );
    $query = $this->db->where('time <= ', strtotime($todate) + 3600 * 24);
    $query = $this->db->order_by('bill_id', 'desc');
    $query = $this->db->count_all_results('gen_bill');
    return $query;
  }

  public function get_con_bills_total_price($store_id, $fromdate, $todate, $max, $page)
  {
    if ($store_id > -1)
      $query = $this->db->query('select sum(total_price) as total_price from gen_bill where store_id=' . $store_id .
                                ' and time >=' . strtotime($fromdate) . ' and ' .
                                'time <=' . (strtotime($todate) + 3600 * 24) 
                                . ' order by bill_id desc' );
    else
      $query = $this->db->query('select sum(total_price) as total_price from gen_bill where 
                                time >=' . strtotime($fromdate) . ' and ' .
                                'time <=' . (strtotime($todate) + 3600 * 24) 
                                . ' order by bill_id desc' );
    return $query->row_array();
  }

  public function get_bill($bill_id)
  { 
    $query = $this->db->query('select * from gen_bill where bill_id = ' . $bill_id);
    return $query->result_array();
  }

  public function new_bill($userid)
  {
    $blackList = array('oP7fjtx0NxenrzfsJVdbBt8o0tB8');
    foreach ($blackList as $item) {
      if ($userid==$item)
        return 0;
    }
    $billjson = $this->input->post("billjson");
    $detail = json_decode($billjson, true);
    $name = $this->input->post("fname");
    $phone = $this->input->post("phone");
    $address = $this->input->post("address");
    $dorm = $this->input->post("dorm");
    $note = $this->input->post("note");
    $total_price = $detail['total_price'];
    $store_id = $detail['store_id'];

    $store_bottom_price = $this->store_model->get_store_bottom_price($store_id);
    $double_total_price = floatval($total_price);
    $double_store_bottom_price = floatval($store_bottom_price);
    $error = 0.001;

    if ($double_total_price<$double_store_bottom_price-$error)
    {
      
    }

    if ($billjson == "" or $name == "" or $phone == "" or $address == "" or $userid == "")
    {
      return 0;
    }//由于信息不完整导致下单失败

    $query = $this->db->query("select * from gen_bill where bill_json = '" . $billjson . "' order by time desc limit 1");
    $query = $query->row_array();
    $time = time();
    if ($query['name'] == $name and $query['phone'] == $phone and $query['address'] == $address 
        and $query['note'] == $note and $time - $query['time'] <= 30)
      return 1;//由于重复下单导致的下单失败

    $data = array(
      'bill_json' => $billjson,
      'name'  => $name,
      'phone' => $phone,
      'address' => $address,
      'dorm' => $dorm,
      'note' => $note,
      'time' => time(),
      'store_id' => $detail["store_id"],
      'print_state' => 0,// 0 present not print,1 present printed
      'send_email' => 0,//0 present not send email,1 presnet send already
      'user_id' => $userid,
      'total_price' => $total_price
    );
    $this->db->query("update user set dorm='" . $dorm . "'" . " where user_id='" . $userid . "'");
    $this->db->insert('gen_bill', $data);
    return 2;//下单成功
  }

  public function get_last_unprint_bill($store_id)
  {
    $query = $this->db->query("select * from gen_bill where print_state = 0 and store_id = $store_id order by bill_id limit 1");
    if ($query == NULL)
    {
      return null;
      /*
      TODO:
      */
    }
    else $content = $query->row_array();
    return $content;
  }

  public function get_unprint_bills($store_id)
  {
    $query = $this->db->query("select * from gen_bill where print_state = 0 and store_id = $store_id order by bill_id");
    if ($query == NULL)
    {
      return null;
      /*
      TODO:
      */
    }
    else $content = $query->result_array();
    return $content;
  }

  public function get_unprint_all_bills()
  {
    $query = $this->db->query("select * from gen_bill inner join store on gen_bill.store_id = store.store_id where gen_bill.print_state = 0 and gen_bill.send_email = 0 and store_printer_IMEI != \"NULL\" and store_printer_IMEI != '' order by gen_bill.bill_id");
    if ($query == NULL)
    {
      return null;
      /*
      TODO:
      */
    }
    else $content = $query->result_array();
    return $content;
  }

  public function update_print_state($bill_id)
  {
    $query = $this->db->query("update gen_bill SET print_state = 1 where bill_id = $bill_id");
    if ($query == NULL)
    {
      /*
      TODO:
      */
    }
  }  

  public function update_send_email($bill_id)
  {
    $query = $this->db->query("update gen_bill SET send_email = 1 where bill_id = $bill_id");
    if ($query == NULL)
    {
      /*
      TODO:
      */
    }
  }

  public function get_history_bill($user_id, $num, $offset)
  {
    $query = $this->db->where('user_id', $user_id);
    $query = $this->db->order_by('bill_id', 'desc');
    $query = $this->db->get('gen_bill', $num, $offset);
    return $query->result_array();
  }  

  public function get_history_bill_num($user_id)
  {
    $query = $this->db->query("select count(*) as count from gen_bill where user_id = '" . $user_id . "'");
    return $query->row_array();
  }

  public function get_dorm_data()
  {
    $query = $this->db->query("select dorm,count(*) as count from gen_bill where dorm is not null group by dorm order by dorm");
    return $query->result_array();
  }

  public function get_dorm_data_count()
  {
    $query = $this->db->query("select count(*) as total from (select dorm,count(*) as count from gen_bill where dorm is not null group by dorm order by dorm) as tb1");
    return $query->row_array();
  }

  public function get_shop_data($store_id)
  {
    $query = $this->db->query("select time from gen_bill where store_id='" . $store_id . "' order by time desc");
    return $query->result_array();
  }

}